package com.example.battery;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class PowerSaveSettingsActivity extends Activity {
	
	public static final String Pref_Names = "battery_doctor";
	
	public static final String wifi_key = "wifi";
	public static final String bluetooth_key = "bluetooth";
	public static final String brightness_key = "brightness";
	public static final String vibration_key = "vibration";
	public static final String mobile_key = "mobiledata";
	
	public boolean wifi_enabled = false;
	public boolean bluetooth_enabled = false;
	public boolean brightness_enabled = false;
	public boolean vibration_enabled = false;
	public boolean mobiledata_enabled = false;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.powers);
		
		getSavedSettings();
		 portrait(null);
		
		CheckBox wifi,bluetooth,brightness,dataconnection,vibration;
		
		wifi = (CheckBox)findViewById(R.id.checkBox_wifi);
	    bluetooth = (CheckBox)findViewById(R.id.checkBox_bluetooth);
	    brightness = (CheckBox)findViewById(R.id.checkBox_brightness);
	    dataconnection = (CheckBox)findViewById(R.id.checkBox_dataconnection);
	    vibration = (CheckBox)findViewById(R.id.checkBox_Vibration);
	    
	    wifi.setChecked(wifi_enabled);
	    bluetooth.setChecked(bluetooth_enabled);
	    brightness.setChecked(brightness_enabled);
	    dataconnection.setChecked(mobiledata_enabled);
	    vibration.setChecked(vibration_enabled);
	   
		
	       wifi.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				wifi_enabled = isChecked;
				
			}
		   });
	       
	       bluetooth.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					bluetooth_enabled = isChecked;
				}
			});
	       brightness.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					brightness_enabled = isChecked;
				}
			});
	       vibration.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					vibration_enabled = isChecked;
				}
			});
	      
			
	       dataconnection.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					mobiledata_enabled = isChecked;
					
					
				 }
			  });
	       }
	
 public void portrait(View v)
 
 {
	 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	
	 
 }
		
	

		public void savebutton (View v){
			SharedPreferences preferences = getApplicationContext().getSharedPreferences(Pref_Names, MODE_PRIVATE);
			SharedPreferences.Editor editor = preferences.edit();
			
			editor.putBoolean(wifi_key, wifi_enabled);
			editor.putBoolean(bluetooth_key, bluetooth_enabled);
			editor.putBoolean(brightness_key, brightness_enabled);
			editor.putBoolean(mobile_key, mobiledata_enabled);
			editor.putBoolean(vibration_key, vibration_enabled);			
			editor.commit();
			
			Toast.makeText(getApplicationContext(), "Saved Successfully...", Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(getApplicationContext(),MainActivity.class);
			startActivity(i);
			finish();
			
      }
		
	public void getSavedSettings()
	{
		SharedPreferences preferences = getApplicationContext().getSharedPreferences(Pref_Names, MODE_PRIVATE);
		wifi_enabled = preferences.getBoolean(wifi_key, false);
		bluetooth_enabled = preferences.getBoolean(bluetooth_key, false);
		brightness_enabled = preferences.getBoolean(brightness_key, false);
		vibration_enabled = preferences.getBoolean(vibration_key, false);
		mobiledata_enabled = preferences.getBoolean(mobile_key, false);
	}
   }
